﻿using ProgramCat.Properties;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ProgramCat.FiddlerExtension
{
    /// <summary>
    /// 全局配置类 单例
    /// </summary>
    public class Config:Singleton<Config>
    {
        /// <summary>
        /// js智能提示
        /// </summary>
        private string JsAutoCompletionText = "";
        /// <summary>
        /// 主页的URL
        /// </summary>
        private string IndexUrlText = "";
        /// <summary>
        /// 配置的目录
        /// </summary>
        private string configPath = "";
        private Config() { }
        public override void OnSingletonInit()
        {
            configPath = Directory.GetCurrentDirectory() + @"\Scripts\ProgramCat\Config\";
            GetJsAutoCompletion();

        }
        public void Init() { }
        /// <summary>
        /// 获得JS自动补全
        /// </summary>
        /// <returns></returns>
        public string GetJsAutoCompletion()
        {
            string JsAutoCompletionPath = configPath + "JsAutoCompletion.txt";
            if (JsAutoCompletionText == "")
            {
                if (!Directory.Exists(configPath))//如果不存在就创建 dir 文件夹  
                {
                    Directory.CreateDirectory(configPath);
                }
                if (!File.Exists(JsAutoCompletionPath))
                {
                    JsAutoCompletionText = Resources.JsAutoCompletion;
                    File.WriteAllText(JsAutoCompletionPath, Resources.JsAutoCompletion);
                }
                else
                {
                    JsAutoCompletionText = File.ReadAllText(JsAutoCompletionPath);
                }
                JsAutoCompletionText = JsAutoCompletionText.Replace("\r\n", " ");
            }
            return JsAutoCompletionText;
        }

        /// <summary>
        /// 获得主页的地址
        /// </summary>
        /// <returns></returns>
        public string GetIndexUrl()
        {
            string IndexUrlPath = configPath + "index.txt";
            if (IndexUrlText == "")
            {
                if (!Directory.Exists(configPath))//如果不存在就创建 dir 文件夹  
                {
                    Directory.CreateDirectory(configPath);
                }
                if (!File.Exists(IndexUrlPath))
                {
                    IndexUrlText = "http://fiddler.waigua.space/about";
                    File.WriteAllText(IndexUrlPath, IndexUrlText);
                }
                else
                {
                    IndexUrlText = File.ReadAllText(IndexUrlPath);
                }
            }
            return IndexUrlText;
        }
    }
}
